GIT_HASH?=$(shell git rev-parse --short HEAD)
GIT_TAG?=$(shell git tag | tail -1)
GIT_BRANCH?=$(shell git rev-parse --abbrev-ref HEAD)
GIT_BRANCH_CLR?=$(shell echo "$(GIT_BRANCH)" | sed -r 's/\//-/g')
OS=$(shell go env GOOS)
CONTROLLER_GEN=$(shell pwd)/tools/controller-gen-$(OS)
REPORT_TOOL=$(shell pwd)/tools/go-test-report-$(OS)
BASE_IMG=
BUILD_IMG=golang:1.21.5
DOCKER_TAG_PREFIX=
GOMOD?=mod
define upcounter
count=$(shell cat .count.inc) ;\
count=$$(( count + 1 )) ;\
echo $$count ;\
echo $$count > .count.inc ;
endef

.PHONY: upcounter
upcounter:
	$(call upcounter)

all: build

local: pr-build test

info:
	echo "$(GOMOD)"

fmt:
	git config --global --add safe.directory /go/src && go fmt -mod=$(GOMOD) ./...

pr-build: fmt
	git config --global --add safe.directory /go/src && CGO_ENABLED=0 go build -mod=$(GOMOD) -o bin/ ./cmd/...

test:
	go test -mod=$(GOMOD) -failfast -v ./... -coverprofile cov.out

install:
	go install ./cmd/...

linux:
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go install -mod=vendor -ldflags "-s" -installsuffix cgo -v ./cmd/...

docker: upcounter docker-kubelatte

docker-kubelatte:
	mkdir -p gocache-for-docker
	docker build \
	--build-arg base_img=$(BASE_IMG) \
	-f docker/kubelatte/Dockerfile -t $(DOCKER_TAG_PREFIX)kubelatte:$(GIT_BRANCH_CLR)-0.$(shell cat .count.inc) .

docker-publish:
	docker push $(DOCKER_TAG_PREFIX)kubelatte:$(GIT_BRANCH_CLR)-0.$(shell cat .count.inc)

.PHONY: build
build: ## Build manager binary in docker
	docker run --rm -v $(shell pwd):/go/src/ -w /go/src/ $(BUILD_IMG) make GOMOD=$(GOMOD) pr-build test

.PHONY: proto
proto:
	cd pkg/services/configsloader && \
	protoc --go_out=. --go_opt=paths=source_relative \
        --go-grpc_out=. --go-grpc_opt=paths=source_relative \
        configs_loader.proto

update:
	go mod vendor

dev:
	make GOMOD=vendor docker

dev-build:
	make GOMOD=vendor build